#include "stdafx.h"
#include "GameplayState.h"
#include "BaseObject.h"
#include "TextureManager.h"
#include "dxdebug.h"
#include "PauseState.h"
#include "DebugObject.h"
#include "DirectInput.h"
#include "Math Functions.h"
#include "Camera.h"
#include "FModManager.h"
#include "DialogueManager.h"
#include "DialogueSequence.h"
#include "TalkingDialogueBox.h"
#include "CombatState.h"
#include "Player.h"
#include "Base Creature.h"
#include "BogRaiders.h"

GameplayState::GameplayState(void)
{
}

GameplayState::~GameplayState(void)
{
}

void GameplayState::Init()
{
	m_nDialogueFlags = 0;
	this->m_vNPCs.clear();
	//this->CreateEnvironment();	//Create the environment (Debug)

	this->m_bInputDialougeBuffer = false;	//Not pushing the talk button
	this->m_bIsTalking = false;				//Not Talking
	this->m_nNumNPCs = 3;

	FModManager::GetInstance()->PlaySound(FModManager::GetInstance()->LoadSound("resources/Waterflame - Swamplands.mp3"));

	DialogueManager::GetInstance()->CreateAllDialogue();
	

	//HUD loading
	BaseObject2D* pSpells = new BaseObject2D();
	pSpells->SetTextureID(TextureManager::GetInstance()->LoadTexture("resources\\allspells.png"));
	ObjectManager::GetInstance()->AddObject(pSpells);
	pSpells->SetPosition(350,500);
	pSpells->SetScale(1.0f, 1.0f);
	m_pSpells = pSpells;
	SAFE_RELEASE(pSpells);

	BaseObject2D* pCursor = new BaseObject2D();
	pCursor->SetTextureID(TextureManager::GetInstance()->LoadTexture("resources\\gamecursor.png"));

	ObjectManager::GetInstance()->AddObject(pCursor);
	pCursor->SetPosition(500,350);
	pCursor->SetScale(1.0f, 1.0f);
	m_pCursor = pCursor;
	SAFE_RELEASE(pCursor);

	m_pSequence = NULL;

	Player* pPlayer = new Player();
	pPlayer->Init();

	Game::GetInstance()->SetPlayer(pPlayer);
	pPlayer->SetPosition(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
	ObjectManager::GetInstance()->AddObject(pPlayer);

	CCamera::GetInstance()->SetCameraMode(OPEN_WORLD_CAMERA);

	CrtBogRaiders* m_Tree1 = new CrtBogRaiders();
 	m_Tree1->LoadMesh("resources\\robot-whitey.x");
 	m_Tree1->SetPosition(D3DXVECTOR3(300.0f,0.0f,0.0f));
 	m_Tree1->SetVelocity(D3DXVECTOR3(0.0f,0.0f,0.0f));
	m_Tree1->Create();
	ObjectManager::GetInstance()->AddObject(m_Tree1);



	m_Tree = new BaseCreature();
 	m_Tree->LoadMesh("resources\\zone1.x");
	m_Tree->SetPosition(D3DXVECTOR3(0.0f,0.0f,0.0f));
	m_Tree->SetVelocity(D3DXVECTOR3(0.0f,0.0f,0.0f));
}

#define TALKED_TO_NPC_1_START 1
#define KILLED_NPC_2 2

#define MOUSE_SPEED 70.0f
void GameplayState::Update(float _dt)
{
	m_Tree->Update(_dt);
	//Switching states temp
	if(DirectInput::GetInstance()->KeyPressed(DIK_5))
	{
		StateManager::GetInstance()->ChangeState(CombatState::GetInstance());
		return;
	}

	D3DXVECTOR3 vMousePos = D3DXVECTOR3(CCamera::GetInstance()->m_mouseDiff[0] * MOUSE_SPEED, CCamera::GetInstance()->m_mouseDiff[1] * MOUSE_SPEED, 0.0f);
	D3DXVECTOR3 vCursorpos = m_pCursor->GetPosition();
	m_pCursor->SetPosition(vCursorpos.x + vMousePos.x, vCursorpos.y + vMousePos.y);

	if(DirectInput::GetInstance()->KeyPressed(DIK_P))
		StateManager::GetInstance()->Push(PauseState::GetInstance());
	
	//Check to see if player is looking at an NPC and within range to talk
	// Will also handle the Dialogue
	//this->CheckNPCs();
	
}

void GameplayState::Render()
{
	ObjectManager::GetInstance()->RenderObjects();
	m_Tree->Render();
}

void GameplayState::Shutdown()
{
	ObjectManager::GetInstance()->RemoveAllObjects();
	FModManager::GetInstance()->ShutdownFModManager();
}

void GameplayState::CreateEnvironment()
{
		//House 1
		CDebugAABB* pHouse = new CDebugAABB();
		DWORD color; color = 0xFF0000;
		D3DXVECTOR3 pos; pos.x = pos.z = 0.0f; pos.y = 150.0f;
		D3DXVECTOR3 dim; dim.x = 200.0f; dim.y = 300.0f; dim.z = 550.0f;

		pHouse->CreateBox(pos, dim, color);
		ObjectManager::GetInstance()->AddObject(pHouse);
		pHouse->Release();

		//House 2
		pos.x = -800; pos.y = 150.0f; pos.z = 500.0f;
		dim.x = 200.0f; dim.y = 300.0f; dim.z = 550.0f;

		pHouse = new CDebugAABB();
		pHouse->CreateBox(pos, dim, color);
		ObjectManager::GetInstance()->AddObject(pHouse);
		pHouse->Release();

		//House 3
		pos.x = 0.0f; pos.y = 150.0f; pos.z = -600.0f;
		dim.x = 200.0f; dim.y = 300.0f; dim.z = 250.0f;

		pHouse = new CDebugAABB();
		pHouse->CreateBox(pos, dim, color);
		ObjectManager::GetInstance()->AddObject(pHouse);
		pHouse->Release();

		//House 4
		pos.x = -800.0f; pos.y = 150.0f; pos.z = -900.0f;
		dim.x = 200.0f; dim.y = 300.0f; dim.z = 450.0f;

		pHouse = new CDebugAABB();
		pHouse->CreateBox(pos, dim, color);
		ObjectManager::GetInstance()->AddObject(pHouse);
		pHouse->Release();

		//NPC 1
		color = 0x00FF00;
		pos.x = -150.0f; pos.y = 25.0f; pos.z = 0.0f;
		dim.x = 20.0f; dim.y = 50.0f; dim.z = 20.0f;

		pHouse = new CNPC();
		pHouse->CreateBox(pos, dim, color);
		pHouse->m_eType = OBJECTNPC;
		ObjectManager::GetInstance()->AddObject(pHouse);
		this->m_vNPCs.push_back(pHouse);
		pHouse->Release();

		//NPC 2
		pos.x = -950.0f; pos.y = 25.0f; pos.z = -300.0f;
		dim.x = 20.0f; dim.y = 50.0f; dim.z = 20.0f;

		pHouse = new CNPC();
		pHouse->CreateBox(pos, dim, color);
		pHouse->m_eType = OBJECTNPC;
		ObjectManager::GetInstance()->AddObject(pHouse);
		this->m_vNPCs.push_back(pHouse);
		pHouse->Release();

		//NPC 3
		pos.x = -650.0f; pos.y = 25.0f; pos.z = -800.0f;
		dim.x = 20.0f; dim.y = 50.0f; dim.z = 20.0f;

		pHouse = new CNPC();
		pHouse->CreateBox(pos, dim, color);
		pHouse->m_eType = OBJECTNPC;
		ObjectManager::GetInstance()->AddObject(pHouse);
		this->m_vNPCs.push_back(pHouse);
		pHouse->Release();

}
void GameplayState::CheckNPCs()
{
	//Creating Camera's ray
	vec3f s, p;
	D3DXVECTOR3 temp = CCamera::GetInstance()->GetPosition();
	s.x = temp.x; s.y = temp.y; s.z = temp.z;
	temp = CCamera::GetInstance()->GetLook(); 
	p.x = temp.x; p.y = temp.y; p.z = temp.z;
	p.scale(500.0f);
	p = s + p;

	//Plane and CP used as fillers
	vec3f min, max, CP; Plane plane; 

	if(DirectInput::GetInstance()->KeyPressed(DIK_SPACE))
	{
 		for(unsigned int i = 0; i < m_nNumNPCs; i += 1)
		{
			//Get the NPC Box

			AABB npc; 
			npc = CreateAABB(m_vNPCs[i]->GetPos(), m_vNPCs[i]->GetDim());

			//if(SegmentToBoxIntersection(s, p, &npc, plane, CP))
			if(Get3dDistance(CCamera::GetInstance()->GetPosition(),m_vNPCs[i]->GetPos()) < 55.0f)
			{
				//Dialogue testing
				//npc 1
				TalkingDialogueBox* pBox = NULL;
				if(m_pSequence == NULL)
				{
					switch (i)
					{
					case 0:
						{			
							m_nDialogueFlags |= TALKED_TO_NPC_1_START;

							m_pSequence = DialogueManager::GetInstance()->npc_1_part_1;
							pBox = (TalkingDialogueBox*)m_pSequence->m_vDialogueBoxes[m_pSequence->m_nCurrentDialogueBox];
							pBox->ResetCharacters();
							ObjectManager::GetInstance()->AddObject((BaseObjectBase*)m_pSequence);

							break;
						}
					case 1:
						{
							m_pSequence = DialogueManager::GetInstance()->npc_2_part_1;
							pBox = (TalkingDialogueBox*)m_pSequence->m_vDialogueBoxes[m_pSequence->m_nCurrentDialogueBox];
							pBox->ResetCharacters();
							ObjectManager::GetInstance()->AddObject((BaseObjectBase*)m_pSequence);
							break;
						}
					case 2:
						{
							m_pSequence = DialogueManager::GetInstance()->npc_3_part_1;
							pBox = (TalkingDialogueBox*)m_pSequence->m_vDialogueBoxes[m_pSequence->m_nCurrentDialogueBox];
							pBox->ResetCharacters();
							ObjectManager::GetInstance()->AddObject((BaseObjectBase*)m_pSequence);
							break;
						}
					}

					i = m_nNumNPCs;
				}
			}
			else if (m_pSequence)
			{
				if(m_pSequence == DialogueManager::GetInstance()->npc_2_part_1)
				{
					if(m_nDialogueFlags & TALKED_TO_NPC_1_START)
					{
						if(m_pSequence->NextDialogue())
							m_pSequence = NULL;
					}
					else
						m_pSequence->EndDialogue();
				}
				else if(m_pSequence != DialogueManager::GetInstance()->npc_2_part_1)
				{
					//will return true if there is no more dialogue
					if(m_pSequence->NextDialogue())
						m_pSequence = NULL;
				}
			}
		}	
	}
}